import 'package:flutter/material.dart';
import 'package:flutter_yishop/config/EventBus.dart';
import 'package:flutter_yishop/constants/Constants.dart';
import 'package:flutter_yishop/services/UserService.dart';
import 'package:flutter_yishop/utils/screen/ScreenAdapter.dart';
import 'package:flutter_yishop/utils/toast_util.dart';

class Me extends StatefulWidget {
  Me({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _Me();
  }
}

class _Me extends State<Me> {
  bool isLogin = false;
  List userInfo = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getUserInfo();
    eventBus.on<UserEvent>().listen((event) {
      this._getUserInfo();
    });
  }

  _getUserInfo() {
    bool isLogin = UserService.getUserLoginState();
    List userInfo = UserService.getUserInfo();
    if(mounted){
      setState(() {
        this.isLogin = isLogin;
        this.userInfo = userInfo;
      });
    }

  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenAdapter.init(context);
    return Scaffold(
        // appBar: AppBar(
        //   title: Text("用户中心"),
        // ),
        body: ListView(
      children: <Widget>[
        GestureDetector(
          child: Container(
            height: ScreenAdapter.height(220),
            width: double.infinity,
            decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage('assets/images/user_bg.jpg'),
                    fit: BoxFit.cover)),
            child: Row(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
                  child: ClipOval(
                    child: Image.asset(
                      'assets/images/user.png',
                      fit: BoxFit.cover,
                      width: ScreenAdapter.width(100),
                      height: ScreenAdapter.width(100),
                    ),
                  ),
                ),
                isLogin?
                Expanded(
                  flex: 1,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text("用户名：${userInfo[0]["username"]}",
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: ScreenAdapter.size(32))),
                      Text("普通会员",
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: ScreenAdapter.size(24))),
                    ],
                  ),
                ): Expanded(
                  flex: 1,
                  child: Text("登录/注册", style: TextStyle(color: Colors.white)),
                )
              ],
            ),
          ),
          onTap: () {
            isLogin?null:Navigator.of(context).pushNamed(Constants.LOGIN);
          },
        ),
        ListTile(
          leading: Icon(Icons.assignment, color: Colors.red),
          title: Text("全部订单"),
          onTap: (){
            Navigator.of(context).pushNamed(Constants.ORDER);
          },
        ),
        Divider(),
        ListTile(
          leading: Icon(Icons.payment, color: Colors.green),
          title: Text("待付款"),
          onTap: (){
            ToastUtil.showToasts("待付款");
          },
        ),
        Divider(),
        ListTile(
          leading: Icon(Icons.local_car_wash, color: Colors.orange),
          title: Text("待收货"),
          onTap: (){
            ToastUtil.showToasts("待收货");
          },
        ),
        Container(
            width: double.infinity,
            height: 10,
            color: Color.fromRGBO(242, 242, 242, 0.9)),
        ListTile(
          leading: Icon(Icons.favorite, color: Colors.lightGreen),
          title: Text("我的收藏"),
          onTap: (){
            ToastUtil.showToasts("我的收藏");
          },
        ),
        Divider(),
        ListTile(
          leading: Icon(Icons.people, color: Colors.black54),
          title: Text("在线客服"),
          onTap: (){
            ToastUtil.showToasts("在线客服");
          },
        ),
        Divider(),
        isLogin?Column(
          children: [
            ListTile(
              leading: Icon(Icons.login_outlined, color: Colors.redAccent),
              title: Text("退出登录"),
              onTap: (){
                UserService.loginOut();
                _getUserInfo();
              },
            ),
            Divider(),
          ],
        ):Container(),
      ],
    ));
  }
}
